<?php


namespace app\lunwen\controller;


use app\lunwen\model\TeacherUploadFileModel;
use cmf\controller\HomeBaseController;

class UploadServerController extends HomeBaseController
{
    public function index()
    {
        //echo $filename = $_FILES["file"]["name"];
        //echo $_FILES["file"]["type"];

        //判断文件上传是否出错
        if($_FILES["file"]["error"])
        {
            echo $_FILES["file"]["error"];
        }
        else
        {
            //控制上传的文件类型，大小
            if($_FILES["file"]["type"]=="image/jpeg"
                ||$_FILES["file"]["type"]=="image/jpg"
                ||$_FILES["file"]["type"]=="image/png"
                &&$_FILES["file"]["size"]<1024000
                || $_FILES["file"]["type"]=="application/vnd.openxmlformats-officedocument.wordprocessingml.document")
            {
                //找到文件存放位置，注意tp5框架的相对路径前面不用/
                //这里的filename进行了拼接，前面是路径，后面从date开始是文件名
                //我在static文件下新建了一个file文件用来存放文件，要注意自己建一个文件才能存放传过来的文件
                $filename = "static/file/".$_FILES["file"]["name"];

                //判断文件是否存在
                if (file_exists($filename))
                {
                    echo "<script>alert('该文件已存在！')</script>";
                    $allFile= new TeacherUploadFileModel();
                    $data = $allFile
                        ->whereTime('create_time','>',session('beforeYear'))
                        ->whereTime('create_time','<',session('afterYear'))
                        ->all();
                    $this->assign('title',$data);
                    return $this->fetch(':teacherApplyBest');
                }
                else
                {
                    //保存文件
                    //move_uploaded_file是php自带的函数，前面是旧的路径，后面是新的路径
                    move_uploaded_file($_FILES["file"]["tmp_name"],$filename);

                    $fileNameString = $_FILES["file"]["name"];
                    echo $stuId = strstr($fileNameString,'_',true);
                    echo '---';
                    $data = strstr($fileNameString,'_');
                    $beformStuName = strstr($data,'.',true);
                    echo $stuname = substr("$beformStuName", 1, strlen($beformStuName));
                    //echo $_FILES["file"]["name"];

                    //查询提交文件表 查询是否有这学号的文件 有责提示 重复
                    $teacherFile = new TeacherUploadFileModel();
                    $File = $teacherFile->where("stu_id",$stuId)->find();
                    if ($File==null) {
                        $file = new TeacherUploadFileModel();
                        //teacher_name teacher_id file_name file_path create_time stu_id stu_name
                        $file->teacher_name = session('teacher_name');
                        $file->teacher_id = session('teacher_id');
                        $file->file_name = $_FILES["file"]["name"];
                        $file->file_path = $filename;


                        $file->status = "导师已提交";

                        //????????????????????????????????????
                        $file->stu_id = $stuId;
                        $file->stu_name = $stuname;
                        $file->save();
                        echo "可以上传";

                        //查找出所有的已提交 传回
                        $allFile= new TeacherUploadFileModel();
                        $data = $allFile ->whereTime('create_time','>',session('beforeYear'))
                            ->whereTime('create_time','<',session('afterYear'))
                            ->all();
                        $this->assign('title',$data);
                        return $this->fetch(':teacherApplyBest');
                    }
                    else{
                        $this->assign("msg","不能重复上传");
                        echo "不能重复上传";
                        //查找出所有的已提交 传回
                        $allFile= new TeacherUploadFileModel();
                        $data = $allFile ->whereTime('create_time','>',session('beforeYear'))
                            ->whereTime('create_time','<',session('afterYear'))
                            ->all();
                        $this->assign('title',$data);
                        return $this->fetch(':teacherApplyBest');
                    }
                }
            }
            else
            {
                echo "文件类型不正确！";
            }
        }

    }
    }